package com.example.wangzh.mytest;

import com.example.wangzh.mytest.base.BaseActivity;
import com.example.wangzh.mytest.databinding.ActivityTransformandmatrixTest1Binding;

public class TransformAndMatrixTest1Activity extends BaseActivity<ActivityTransformandmatrixTest1Binding> {
    @Override
    protected void initView() {
        setupClickListeners();
    }
    private void setupClickListeners() {
        mBinding.btnTest1.setOnClickListener(v -> testDefaultPivot());
        mBinding.btnTest2.setOnClickListener(v -> testZeroPivot());
        mBinding.btnReset.setOnClickListener(v -> resetView());
    }

    private void testDefaultPivot() {
        resetView();

        StringBuilder result = new StringBuilder();
        result.append("=== 测试1: 默认pivot点缩放 ===\n");

        // 获取默认pivot点（视图中心）
        float defaultPivotX = mBinding.testView.getPivotX();
        float defaultPivotY = mBinding.testView.getPivotY();
        result.append("默认pivot点: (").append(defaultPivotX).append(", ").append(defaultPivotY).append(")\n");

        // 记录初始状态
        result.append("缩放前:\n");
        result.append("ScaleX: ").append(mBinding.testView.getScaleX()).append("\n");
        result.append("ScaleY: ").append(mBinding.testView.getScaleY()).append("\n");
        result.append("TranslationX: ").append(mBinding.testView.getTranslationX()).append("\n");
        result.append("TranslationY: ").append(mBinding.testView.getTranslationY()).append("\n");
        result.append("X: ").append(mBinding.testView.getX()).append("\n");
        result.append("Y: ").append(mBinding.testView.getY()).append("\n");
        result.append("matrix: ").append(mBinding.testView.getMatrix()).append("\n");

        // 设置缩放
        mBinding.testView.setScaleX(2.0f);
        mBinding.testView.setScaleY(1.5f);

        result.append("缩放后 (setScaleX(2.0f), setScaleY(1.5f):\n");
        result.append("ScaleX: ").append(mBinding.testView.getScaleX()).append("\n");
        result.append("ScaleY: ").append(mBinding.testView.getScaleY()).append("\n");
        result.append("TranslationX: ").append(mBinding.testView.getTranslationX()).append("\n");
        result.append("TranslationY: ").append(mBinding.testView.getTranslationY()).append("\n");
        result.append("X: ").append(mBinding.testView.getX()).append("\n");
        result.append("Y: ").append(mBinding.testView.getY()).append("\n");
        result.append("matrix: ").append(mBinding.testView.getMatrix()).append("\n");
        mBinding.tvResult.setText(result.toString());
    }

    private void testZeroPivot() {
        resetView();

        StringBuilder result = new StringBuilder();
        result.append("=== 测试2: pivot设为(0,0)后缩放 ===\n");

        // 设置pivot点为(0,0)
        mBinding.testView.setPivotX(0f);
        mBinding.testView.setPivotY(0f);

        result.append("设置pivot点为: (0, 0)\n");

        // 记录设置pivot后的状态
        result.append("设置pivot后，缩放前:\n");
        result.append("ScaleX: ").append(mBinding.testView.getScaleX()).append("\n");
        result.append("ScaleY: ").append(mBinding.testView.getScaleY()).append("\n");
        result.append("TranslationX: ").append(mBinding.testView.getTranslationX()).append("\n");
        result.append("TranslationY: ").append(mBinding.testView.getTranslationY()).append("\n");
        result.append("X: ").append(mBinding.testView.getX()).append("\n");
        result.append("Y: ").append(mBinding.testView.getY()).append("\n");
        result.append("matrix: ").append(mBinding.testView.getMatrix()).append("\n");
        // 设置缩放
        mBinding.testView.setScaleX(2.0f);
        mBinding.testView.setScaleY(1.5f);

        result.append("缩放后 (setScaleX(2.0f), setScaleY(1.5f):\n");
        result.append("ScaleX: ").append(mBinding.testView.getScaleX()).append("\n");
        result.append("ScaleY: ").append(mBinding.testView.getScaleY()).append("\n");
        result.append("TranslationX: ").append(mBinding.testView.getTranslationX()).append("\n");
        result.append("TranslationY: ").append(mBinding.testView.getTranslationY()).append("\n");
        result.append("X: ").append(mBinding.testView.getX()).append("\n");
        result.append("Y: ").append(mBinding.testView.getY()).append("\n");
        result.append("matrix: ").append(mBinding.testView.getMatrix()).append("\n");
        // 额外测试：观察视觉位置变化
        result.append("观察结果:\n");
        result.append("- X,Y值不会因变换而改变\n");
        result.append("- Translation值通常不会因缩放而自动改变\n");
        result.append("- 因缩放中心变化导致的位置变化主要通过matrix来记录和体现");

        mBinding.tvResult.setText(result.toString());
    }

    private void resetView() {
        mBinding.testView.setScaleX(1.0f);
        mBinding.testView.setScaleY(1.0f);
        mBinding.testView.setTranslationX(0f);
        mBinding.testView.setTranslationY(0f);
        mBinding.testView.setPivotX(mBinding.testView.getWidth() / 2f); // 重置为默认中心点
        mBinding.testView.setPivotY(mBinding.testView.getHeight() / 2f);
    }
}
